package com.niit.mobileDevide.operatesystem;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class OperateSystemDriver {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "User Behavior Analysis by Operating System");

        job.setJarByClass(OperateSystemDriver.class);
        job.setMapperClass(OperateSystemMapper.class);
        job.setReducerClass(OperateSystemReducer.class);
        job.setPartitionerClass(OperateSystemPartitioner.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        job.setNumReduceTasks(2); // 设置reducer的数量为2，对应不同的操作系统分区

        // 输入路径
        Path inputPath = new Path("input/user_behavior_dataset.csv");
        FileInputFormat.addInputPath(job, inputPath);

        // 输出路径
        Path outputPath = new Path("output/operatingsystempartition");
        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(outputPath)) {
            fs.delete(outputPath, true); // 删除已存在的输出目录
        }
        FileOutputFormat.setOutputPath(job, outputPath);

        // 启用详细日志输出
        conf.set("mapreduce.map.log.level", "DEBUG");
        conf.set("mapreduce.reduce.log.level", "DEBUG");

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}